package com.own.business.system.dictionary.base.frame.business.service.impl;

import com.own.business.system.dictionary.base.frame.business.service.SystemDictionaryService;
import com.own.business.system.dictionary.base.frame.entity.bo.SystemDictionaryBo;
import com.own.business.system.dictionary.base.frame.entity.map.SystemDictionaryMapVo;
import com.own.business.system.dictionary.base.frame.entity.po.SystemDictionary;
import com.own.business.system.dictionary.base.frame.entity.query.SystemDictionaryQuery;
import com.own.business.system.dictionary.base.frame.entity.vo.SystemDictionaryVo;
import com.own.business.system.dictionary.base.frame.mapper.SystemDictionaryMapper;
import com.own.component.common.base.service.impl.AbstractBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SystemDictionaryServiceImpl
 *
 * @author chenxueli
 * @date 2023-06-27 15:29:30
 */
@Slf4j
@Service
public class SystemDictionaryServiceImpl extends AbstractBaseService<
        SystemDictionary,
        SystemDictionaryBo,
        SystemDictionaryVo,
        SystemDictionaryMapVo,
        SystemDictionaryQuery,
        SystemDictionaryMapper
        > implements SystemDictionaryService {

    @Resource
    private SystemDictionaryMapper systemDictionaryMapper;

    /**
     * 根据数据字典类型的code查询
     *
     * @param code code值
     * @return 返回列表
     */
    @Override
    public List<SystemDictionaryMapVo> mapByDictionaryCode(String code) {
        var wrapper = lambdaQueryWrapper().eq(SystemDictionary::getItemCode, code);
        var dictionary = systemDictionaryMapper.selectOne(wrapper);
        if (dictionary == null) {
            return new ArrayList<>();
        }
        // 查询子信息
        var wrapper2 = lambdaQueryWrapper()
                .eq(SystemDictionary::getFkParentId, dictionary.getId())
                .ne(SystemDictionary::getState, 2);
        var list = systemDictionaryMapper.selectList(wrapper2);
        // 处理信息
        return list.stream().map(SystemDictionaryMapVo::new).collect(Collectors.toList());
    }

    /**
     * 根据name和rootCode获取id
     *
     * @param name     名称
     * @param rootCode 根code
     * @return id
     */
    @Override
    public Long getIdByNameAndRootCode(String name, String rootCode) {
        var wrapper = lambdaQueryWrapper().eq(SystemDictionary::getRfRootCode, rootCode)
                .eq(SystemDictionary::getItemName, name);
        var dictionary = systemDictionaryMapper.selectOne(wrapper);
        return dictionary == null ? null : dictionary.getId();
    }

    /**
     * 根据数据字典类型的code查询
     *
     * @param code code值
     * @return 返回列表
     */
    @Override
    public SystemDictionary getByCode(String code) {
        var wrapper = lambdaQueryWrapper()
                .eq(SystemDictionary::getItemCode, code)
                .ne(SystemDictionary::getState, 2);
        return getOneByWrapper(wrapper);
    }

}
